﻿using System;
using System.Collections.Generic;
using System.Text;
using Vbyte.SharpOrm.Adapter;
using Vbyte.SharpOrm.SqlDb;

namespace Vbyte.SharpOrm.SqLite
{
    /// <summary>
    /// SqLite的SQL构建适配
    /// </summary>
    public class LiteSqlBuilder : SqlBuilderBase
    {
        /// <summary>
        /// 是否实现了约束的表达式语句构建
        /// </summary>
        /// <returns></returns>
        public override bool HasImplementConstraintExp()
        {
            return true;
        }

        /// <summary>
        /// 构建SQL约束表达式
        /// </summary>
        /// <param name="build">实现了约束表达式构建的实例</param>
        /// <param name="dbOwner">数据库所有者占位前缀</param>
        /// <param name="tableName">数据库表名</param>
        /// <param name="columnName">涉及到的列名</param>
        /// <returns>最终的约束表达式语句</returns>
        public override string BuildConstraintExpression(ISqlConstraintExpBuild build, string dbOwner, string tableName, string columnName)
        {
            if (build == null)
            {
                throw new NullReferenceException("SQL约束构建实现不能为null!");
            }
            string exp = build.GetExpression(dbOwner, tableName, columnName);
            exp = exp.Replace("{DBOWNER}", "");
            return exp;
        }

        /// <summary>
        /// 获取常用SQL语句的格式化定义
        /// </summary>
        /// <param name="cmdType">SQL数据表中单行命令类型</param>
        /// <returns>SQL语句的格式化定义</returns>
        public override string GetSqlFormatText(SQLRowCommandType cmdType)
        {
            if (cmdType == SQLRowCommandType.Select)
            {
                return "Select {FieldNamesList} from {VirtualTable} {SqlCondition} {SqlGroupBy} {SqlOrderBy}{SqlLimit}";
            }
			return base.GetSqlFormatText(cmdType);
        }

        /// <summary>
        /// 数据获取限制(TOP)
        /// </summary>
        /// <value></value>
        public override string SqlLimit
        {
             get
            {
                if (base.SqlLimit != string.Empty)
                {
                    base.SqlLimit = String.Concat(" limit ", _maxRows, " ");
                }
                return base.SqlLimit;
            }
            set
            {
                base.SqlLimit = value;
            }
        }

		/// <summary>
        /// 目标SQL构建格式化语句中的关键词集合
        /// </summary>
        /// <returns>关键词集合</returns>
//        public override string[] GetFormatKeys()
//        {
//            return "{SqlLimit} {VirtualTable} {FieldNamesList} {FieldValuesList} {SqlCondition} {SqlGroupBy} {SqlOrderBy} {CustomValueSetList}".Split(' ');
//        }

        /// <summary>
        /// 构建命令适配的SQL文本
        /// </summary>
        /// <param name="entry">ORM操作实例原型</param>
        /// <param name="cmdType">SQL数据表中单行命令类型</param>
        /// <param name="isSingleRow">是否时单行操作，如TOP 1等。</param>
        /// <returns>目标SQL语句</returns>
        public override string BuildCommandText(TableEntry entry, SQLRowCommandType cmdType, bool isSingleRow)
        {
            return base.BuildCommandText(entry, cmdType, isSingleRow);
        }

    }

}
